package com.xu.demo.feature.util;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONReader;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ReadAssetTypeUtil {
    private static final String path = "/static/api/assetType.json";

    /**
     * 读取 json 文件
     * @return jsonArr
     */
    public static List<JSONObject> readFile()  {
        InputStream inputStream;
        BufferedReader br;
        Reader reader;
        try {
            Resource resource = new ClassPathResource(path);

            inputStream = resource.getInputStream();
            reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
            br = new BufferedReader(reader);
        } catch (IOException e) {
            return new ArrayList<>();
        }

        List<JSONObject> arr = new ArrayList<>(11);// 资产类型有11个
        JSONReader jr = new JSONReader(br);
        jr.startArray();
        while(jr.hasNext()){
            JSONObject value = (JSONObject) jr.readObject();
            arr.add(value);
        }
        jr.endArray();

        try {// 释放资源
            inputStream.close();
            reader.close();
            br.close();
        } catch (IOException e) {
            log.error("exception message", e);
        }
        return arr;
    }
}
